<template>
  <TopBanner></TopBanner>
  <MyNav></MyNav>
  <div>
    <!--  新闻中心 -->
    <div class="news_center">
      <div class="part_title">内容分享</div>
      <div class="part_decoration">
        <img src="../../../src/assets/img/xhx_01.jpg" alt="" />
      </div>
      <div class="shareArticle_continer w">
        <!-- 新闻插入样例 -->
        <!-- .slice().reverse() 反转数组 -->
        <div
          class="shareArticle_item"
          v-for="item in articleList.slice().reverse()"
          :key="item.id"
        >
          <div class="shareArticle_inner">
            <div class="shareArticle_mian">
              <div class="shareArticle_title">
                <a @click="goArticle(item.id)">{{ item.title }}</a>
              </div>
              <div class="shareArticle_body">
                {{ item.contentPreview }}
              </div>
              <div class="share_article_info">
                <div>{{ item.author }}</div>
                <div>{{ datePart(item.updatedAt) }}</div>
                <div>
                  <i class="fa fa-thumbs-o-up">&nbsp;{{ item.likeSum }}</i>
                </div>
                <div>
                  <i class="fa fa-eye">&nbsp;{{ item.watchSum }}</i>
                </div>
              </div>
            </div>
            <img src="../../../src/assets/img/news2.jpg" alt="" />
          </div>
        </div>
      </div>
    </div>
    <!-- 固定定位 内容分享 -->
    <div class="share_block">
      <a @click="goEdit" style="cursor: pointer"
        ><img src="../../../src/assets/icon/edit.svg" alt="" /><br />我要分享</a
      >
    </div>
  </div>
</template>

<script setup>
  import { ref } from "vue";
  import { GetAllArticle } from "../../api/ArticleApi";
  import TopBanner from "@components/TopBanner/TopBanner.vue";
  import MyNav from "@components/Nav/MyNav.vue";
  import router from "@router/index";
  // 导入useUserStore
  import useUserStore from "@store/user";
  // 创建文章列表
  const articleList = ref([]);
  // 获取文章信息
  GetAllArticle().then((res) => {
    articleList.value = res;
    console.log(res);
  });

  const goArticle = (id) => {
    console.log("跳转文章id" + id);
    router.push("/news/" + id);
  };
  // function goArticle(id) {
  //   console.log("跳转文章id" + id);
  //   this.$router.push("/news/" + id);
  // }
  function goEdit() {
    const userStore = useUserStore();
    console.log(userStore.getLoginStatus);
    if (!userStore.getLoginStatus) {
      ElMessage.error("您还未登录");
      setTimeout(() => {
        router.push("/login");
      }, 800);
    } else {
      router.push("/edit");
    }
    // router.push("/edit");
  }

  // 时间处理函数
  function datePart(input) {
    return input.slice(0, 10);
  }
</script>

<style>
  .shareArticle_title > a {
    cursor: pointer;
  }
  .fa-thumbs-o-up {
    color: black;
  }
  .test {
    display: block;
  }
  .news_center {
    display: block;
  }
  .part_title {
    text-align: center;
    font-size: 36px;
    margin-top: 40px;
    padding-bottom: 8px;
  }
  .part_decoration {
    position: absolute;
    width: 100%;
  }
  .part_decoration > img {
    display: block;
    margin: 0 auto;
    height: 15px;
  }
  /* 内容分享区域 */
  .shareArticle_continer {
    height: 900px;
    margin: 0 auto;
    margin-top: 20px;
    /* border: 1px solid black; */
  }

  .shareArticle_item {
    display: flex;
    flex-direction: column;
  }

  .shareArticle_item img {
    width: 350px;
    height: 260px;
  }

  .shareArticle_mian {
    width: 740px;
    padding: 0 50px;
    padding-top: 40px;
    height: 290px;
    float: left;
  }

  .shareArticle_title {
    font-size: 24px;
  }
  .shareArticle_title a:hover {
    color: #ff8622;
  }

  .shareArticle_body {
    width: 620px;
    height: 60px;
    margin-top: 55px;
    font-size: 16px;
    line-height: 30px;
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    /* 行数 */
    -webkit-box-orient: vertical;
    text-overflow: ellipsis;
  }

  .share_article_info {
    margin-top: 100px;
    width: 740px;
    height: 30px;
    position: relative;
    /* border: 1px solid black; */
  }
  .share_article_info div {
    float: left;
    padding-left: 25px;
    overflow: hidden;
  }
  .share_article_info div:first-child {
    padding-left: 0;
  }

  .share_article_info div img {
    overflow: hidden;
    margin-top: -33px;
    float: left;
    width: 1em;
    height: 1em;
    vertical-align: -0.15em;
  }
  .share_article_info div img:last-child {
    margin-top: -34px;
  }

  .shareArticle_inner {
    border-bottom: 1px solid black;
  }

  .shareArticle_inner img {
    padding-top: 35px;
    float: right;
    right: 0;
  }

  /* 内容分享区域*/
  .share_block {
    /* 固定定位 */
    position: fixed;
    /* 右侧距离 */
    right: 226px;
    /* 下侧距离 */
    top: 400px;
    /* 宽度 */
    width: 75px;
    /* 高度 */
    height: 75px;
    /* 背景颜色 */
  }
  .share_block img {
    width: 35px;
    height: 35px;
    margin-left: 14px;
  }
  .share_block a:hover {
    color: #ff8622;
  }
</style>
